//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#include <global.h>
#include "modeldataobjectadapters.h"
#include "grouppage.h"
#include "ptpage.h"
#include "uierror.h"
#include <common/restoreerror.h>
#include <common/restorestream.h>
#include <model/root.h>
#include <model/group.h>
#include <model/shooter.h>
#include <model/particletemplate.h>
#include <model/emittertemplate.h>
//
void ModelDataObjectAdapter::initialize_name_to_type()
{
   name_to_type[model::ParticleTemplate::staticMetaObject.className()]=type_particle_template_adapter;
   name_to_type[model::EmitterTemplate::staticMetaObject.className()]=type_emitter_template_adapter;
   name_to_type[model::Shooter::staticMetaObject.className()]=type_shooter_adapter;
   name_to_type[model::Group::staticMetaObject.className()]=type_group_adapter;
}
//
Identified *ModelGroupAdapter::create_object(QObject* p_parent) const
{
   return new model::Group(p_parent);
}
QString ModelGroupAdapter::get_object_name(const Identified *p_object) const
{
   const model::Group *group=qobject_cast<const model::Group*>(p_object);
   Q_ASSERT(group);
   return group->get_name();
}
void ModelGroupAdapter::set_object_name(Identified *p_object,QString p_name) const
{
   model::Group *group=qobject_cast<model::Group*>(p_object);
   Q_ASSERT(group);
   group->set_name(p_name);
}
void ModelGroupAdapter::insert_into_root(Identified *p_object, model::Root *p_root) const
{
   model::Group *group=qobject_cast<model::Group*>(p_object);
   Q_ASSERT(group);
   Q_ASSERT(!p_root->groups.contains(group));
   p_root->groups.insert(group);
}
void ModelGroupAdapter::remove_from_root(Identified *p_object, model::Root *p_root) const
{
   model::Group *group=qobject_cast<model::Group*>(p_object);
   Q_ASSERT(group);
   Q_ASSERT(p_root->groups.contains(group));
   p_root->groups.remove(group);
}
void ModelGroupAdapter::store_object_data(const Identified *p_object, QByteArray &p_bytes) const
{
   const model::Group *object=qobject_cast<const model::Group*>(p_object);
   Q_ASSERT(object);
   p_bytes.clear();
   QDataStream stream(&p_bytes, QIODevice::WriteOnly);
   stream << data_version_1_qt_4_6;
   stream.setVersion(QDataStream::Qt_4_6);
   object->store_data(stream);
}
Identified *ModelGroupAdapter::restore_object(const QByteArray &p_bytes, const IdDictionary &p_dictionary) const 
{
   RestoreStream stream(p_dictionary, p_bytes);
   qint32 version;
   stream >> version;
   if (version != data_version_1_qt_4_6)
      throw RestoreError(qApp->translate("ModelDataObjectAdapters","invalid version of Group data in ModelGroupAdapter::restore_object"));
   stream.setVersion(QDataStream::Qt_4_6);
   model::Group *object = new model::Group;
   object->restore_data(stream);
   return object;
}
unsigned int ModelGroupAdapter::get_adapter_type() const
{
   return type_group_adapter;
}
QString ModelGroupAdapter::get_type_name() const
{
   return qApp->translate("ModelDataObjectAdapters","group");
}
QIcon ModelGroupAdapter::get_type_icon() const
{
   return QIcon(":/typeicon/group");
}
QString ModelGroupAdapter::get_short_name() const
{
   return qApp->translate("ModelDataObjectAdapters","group");
}
ObjectPage *ModelGroupAdapter::create_page(MainView *p_main_view,QWidget *p_parent) const
{
   return new GroupPage(p_main_view,p_parent);
}
//
Identified *ModelShooterAdapter::create_object(QObject* p_parent) const
{
   return 0;//new model::Shooter(p_parent);
}
QString ModelShooterAdapter::get_object_name(const Identified *p_object) const
{
   const model::Shooter *shooter=qobject_cast<const model::Shooter*>(p_object);
   Q_ASSERT(shooter);
   return shooter->get_name();
}
void ModelShooterAdapter::set_object_name(Identified *p_object,QString p_name) const
{
   model::Shooter *shooter=qobject_cast<model::Shooter*>(p_object);
   Q_ASSERT(shooter);
   shooter->set_name(p_name);
}
void ModelShooterAdapter::insert_into_root(Identified *p_object, model::Root *p_root) const
{
   model::Shooter *shooter=qobject_cast<model::Shooter*>(p_object);
   Q_ASSERT(shooter);
   //Q_ASSERT(!p_root->shooters.contains(shooter));
   //p_root->shooters.insert(shooter);
}
void ModelShooterAdapter::remove_from_root(Identified *p_object, model::Root *p_root) const
{
   model::Shooter *shooter=qobject_cast<model::Shooter*>(p_object);
   Q_ASSERT(shooter);
   //Q_ASSERT(p_root->shooters.contains(shooter));
   //p_root->shooters.remove(shooter);
}
void ModelShooterAdapter::store_object_data(const Identified *p_object, QByteArray &p_bytes) const
{
   const model::Shooter *object=qobject_cast<const model::Shooter*>(p_object);
   Q_ASSERT(object);
   p_bytes.clear();
   QDataStream stream(&p_bytes, QIODevice::WriteOnly);
   stream << data_version_1_qt_4_6;
   stream.setVersion(QDataStream::Qt_4_6);
   object->store_data(stream);
}
Identified *ModelShooterAdapter::restore_object(const QByteArray &p_bytes, const IdDictionary &p_dictionary) const 
{
   RestoreStream stream(p_dictionary, p_bytes);
   qint32 version;
   stream >> version;
   if (version != data_version_1_qt_4_6)
      throw RestoreError(qApp->translate("ModelDataObjectAdapters","invalid version of Shooter data in ModelShooterAdapter::restore_object"));
   stream.setVersion(QDataStream::Qt_4_6);
   //TODO
   //model::Shooter *object = new model::Shooter;
   //object->restore_data(stream);
   return 0;//object;
}
unsigned int ModelShooterAdapter::get_adapter_type() const
{
   return type_shooter_adapter;
}
QString ModelShooterAdapter::get_type_name() const
{
   return qApp->translate("ModelDataObjectAdapters","shooter");
}
QIcon ModelShooterAdapter::get_type_icon() const
{
   return QIcon(":/typeicon/shooter");
}
QString ModelShooterAdapter::get_short_name() const
{
   return qApp->translate("ModelDataObjectAdapters","shooter");
}
ObjectPage *ModelShooterAdapter::create_page(MainView *p_main_view,QWidget *p_parent) const
{
   return 0;
}
//
Identified *ModelParticleTemplateAdapter::create_object(QObject* p_parent) const
{
   return new model::ParticleTemplate(p_parent);
}
QString ModelParticleTemplateAdapter::get_object_name(const Identified *p_object) const
{
   const model::ParticleTemplate *particle_template=qobject_cast<const model::ParticleTemplate*>(p_object);
   Q_ASSERT(particle_template);
   return particle_template->get_name();
}
void ModelParticleTemplateAdapter::set_object_name(Identified *p_object,QString p_name) const
{
   model::ParticleTemplate *particle_template=qobject_cast<model::ParticleTemplate*>(p_object);
   Q_ASSERT(particle_template);
   particle_template->set_name(p_name);
}
void ModelParticleTemplateAdapter::insert_into_root(Identified *p_object, model::Root *p_root) const
{
   model::ParticleTemplate *particle_template=qobject_cast<model::ParticleTemplate*>(p_object);
   Q_ASSERT(particle_template);
   Q_ASSERT(!p_root->particle_templates.contains(particle_template));
   p_root->particle_templates.insert(particle_template);
}
void ModelParticleTemplateAdapter::remove_from_root(Identified *p_object, model::Root *p_root) const
{
   model::ParticleTemplate *particle_template=qobject_cast<model::ParticleTemplate*>(p_object);
   Q_ASSERT(particle_template);
   Q_ASSERT(p_root->particle_templates.contains(particle_template));
   p_root->particle_templates.remove(particle_template);
}
void ModelParticleTemplateAdapter::store_object_data(const Identified *p_object, QByteArray &p_bytes) const
{
   const model::ParticleTemplate *object=qobject_cast<const model::ParticleTemplate*>(p_object);
   Q_ASSERT(object);
   p_bytes.clear();
   QDataStream stream(&p_bytes, QIODevice::WriteOnly);
   stream << data_version_1_qt_4_6;
   stream.setVersion(QDataStream::Qt_4_6);
   object->store_data(stream);
}
Identified *ModelParticleTemplateAdapter::restore_object(const QByteArray &p_bytes, const IdDictionary &p_dictionary) const 
{
   RestoreStream stream(p_dictionary, p_bytes);
   qint32 version;
   stream >> version;
   if (version != data_version_1_qt_4_6)
      throw RestoreError(qApp->translate("ModelDataObjectAdapters","invalid version of ParticleTemplate data in ModelParticleTemplateAdapter::restore_object"));
   stream.setVersion(QDataStream::Qt_4_6);
   model::ParticleTemplate *object = new model::ParticleTemplate;
   object->restore_data(stream);
   return object;
}
unsigned int ModelParticleTemplateAdapter::get_adapter_type() const
{
   return type_particle_template_adapter;
}
QString ModelParticleTemplateAdapter::get_type_name() const
{
   return qApp->translate("ModelDataObjectAdapters","particle template");
}
QIcon ModelParticleTemplateAdapter::get_type_icon() const
{
   return QIcon(":/typeicon/particletemplate");
}
QString ModelParticleTemplateAdapter::get_short_name() const
{
   return qApp->translate("ModelDataObjectAdapters","parttempl");
}
ObjectPage *ModelParticleTemplateAdapter::create_page(MainView *p_main_view,QWidget *p_parent) const
{
   return new PTPage(p_main_view,p_parent);
}
//
Identified *ModelEmitterTemplateAdapter::create_object(QObject* p_parent) const
{
   return new model::EmitterTemplate(p_parent);
}
QString ModelEmitterTemplateAdapter::get_object_name(const Identified *p_object) const
{
   const model::EmitterTemplate *emitter_template=qobject_cast<const model::EmitterTemplate*>(p_object);
   Q_ASSERT(emitter_template);
   return emitter_template->get_name();
}
void ModelEmitterTemplateAdapter::set_object_name(Identified *p_object,QString p_name) const
{
   model::EmitterTemplate *emitter_template=qobject_cast<model::EmitterTemplate*>(p_object);
   Q_ASSERT(emitter_template);
   emitter_template->set_name(p_name);
}
void ModelEmitterTemplateAdapter::insert_into_root(Identified *p_object, model::Root *p_root) const
{
   model::EmitterTemplate *emitter_template=qobject_cast<model::EmitterTemplate*>(p_object);
   Q_ASSERT(emitter_template);
   Q_ASSERT(!p_root->emitter_templates.contains(emitter_template));
   p_root->emitter_templates.insert(emitter_template);
}
void ModelEmitterTemplateAdapter::remove_from_root(Identified *p_object, model::Root *p_root) const
{
   model::EmitterTemplate *emitter_template=qobject_cast<model::EmitterTemplate*>(p_object);
   Q_ASSERT(emitter_template);
   Q_ASSERT(p_root->emitter_templates.contains(emitter_template));
   p_root->emitter_templates.remove(emitter_template);
}
void ModelEmitterTemplateAdapter::store_object_data(const Identified *p_object, QByteArray &p_bytes) const
{
   const model::EmitterTemplate *object=qobject_cast<const model::EmitterTemplate*>(p_object);
   Q_ASSERT(object);
   p_bytes.clear();
   QDataStream stream(&p_bytes, QIODevice::WriteOnly);
   stream << data_version_1_qt_4_6;
   stream.setVersion(QDataStream::Qt_4_6);
   object->store_data(stream);
}
Identified *ModelEmitterTemplateAdapter::restore_object(const QByteArray &p_bytes, const IdDictionary &p_dictionary) const 
{
   RestoreStream stream(p_dictionary, p_bytes);
   qint32 version;
   stream >> version;
   if (version != data_version_1_qt_4_6)
      throw RestoreError(qApp->translate("ModelDataObjectAdapters","invalid version of EmitterTemplate data in ModelEmitterTemplateAdapter::restore_object"));
   stream.setVersion(QDataStream::Qt_4_6);
   model::EmitterTemplate *object = new model::EmitterTemplate;
   object->restore_data(stream);
   return object;
}
unsigned int ModelEmitterTemplateAdapter::get_adapter_type() const
{
   return type_emitter_template_adapter;
}
QString ModelEmitterTemplateAdapter::get_type_name() const
{
   return qApp->translate("ModelDataObjectAdapters","emitter template");
}
QIcon ModelEmitterTemplateAdapter::get_type_icon() const
{
   return QIcon(":/typeicon/emittertemplate");
}
QString ModelEmitterTemplateAdapter::get_short_name() const
{
   return qApp->translate("ModelDataObjectAdapters","emittempl");
}
ObjectPage *ModelEmitterTemplateAdapter::create_page(MainView *p_main_view,QWidget *p_parent) const
{
   return 0;
}
